package com.cloudptt.api.product.func.lbs;

import com.cloudptt.api.models.lbs.LocationData;
import com.cloudptt.api.product.config.lbs.LocationConfig;
import com.cloudptt.api.product.func.lbs.server.AbstractLocServer;
import com.cloudptt.api.product.func.lbs.server.BaiduLocServer;
import com.cloudptt.api.product.func.lbs.server.GpsLocServer;
import com.cloudptt.api.product.log.Log;

import android.app.Service;
import android.content.Intent;
import android.database.ContentObserver;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.provider.Settings;

public class LBSService extends Service {

	//	private static final String TAG = "LBSService";

	//	private Context ctx;
//	private McIdInfo mcIdInfo = null;
	
	private LBSBinder lbsbinder;

//	private static int curHeartRate = 0;
//	private boolean isSerialReport = false;
//	private String curMcpttId = null;

	private AbstractLocServer locServer = null;
//	private  LBSOperator operator;

//	private AlarmTimer timer = null;
	
//	private NotifyObserver mNotifyObserver;

	public class LBSBinder extends Binder {

		public LBSService getService() {
			return LBSService.this;
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		Log.i(LBSFunc.Log_Key, "IBinder start lbsservice");
		return lbsbinder;
	}

	@Override
	public void onCreate() {
		Log.i(LBSFunc.Log_Key, "lbs onCreate");

		//		gpsLocClient = (LocationManager) getSystemService(LOCATION_SERVICE);
		lbsbinder = new LBSBinder();
		
		
//		closeNotifyButton();
		
//		mNotifyObserver = new NotifyObserver(null);
		
//		getApplicationContext().getContentResolver().registerCoSntentObserver(Settings.System.getUriFor("main_screen_on_while_notify") , false, mNotifyObserver);
				
	}



	@Override
	public boolean stopService(Intent name) {
		return super.stopService(name);
	}

	@Override
	public void onDestroy() {		

		Log.i(LBSFunc.Log_Key, "on destory lbsservice");
		stopLBS();
		
//		getApplicationContext().getContentResolver().unregisterContentObserver(mNotifyObserver);
	}

	@Override
	public boolean onUnbind(Intent intent) {

		Log.i(LBSFunc.Log_Key, "on onUnbind lbsservice");
		return super.onUnbind(intent);
	}


	public void startLBS(LocationConfig localConfig) {
		Log.i(LBSFunc.Log_Key, "startLBS localConfig = " + localConfig);
		if(localConfig == null
				|| !localConfig.isOpenLocation())
		{
			return;
		}
		Log.d(LBSFunc.Log_Key, "locServer = "+locServer);
		if(locServer != null){
			return;
		}

		if(localConfig.getLocationType() == LocationConfig.Type_GPS)
		{
			locServer = new GpsLocServer();
			locServer.startLBS(this, localConfig);
//			curLocationType = LocationConfig.Type_GPS;
		}
		else if(localConfig.getLocationType() == LocationConfig.Type_Baidu)
		{
			locServer = new BaiduLocServer();
			locServer.startLBS(this, localConfig);
//			curLocationType = LocationConfig.Type_Baidu;
		}
		else
		{
//			curLocationType = LocationConfig.Type_None;
		}
		
//		int timerInterval = localConfig.getTimeInterval();
//		this.timer = new AlarmTimer(LBSService.this,"reportData",timerInterval,this);
//		timer.startRefreshTimer();
	}

	/*public void updateConfig(Context ctx,String mcpttId,LocationConfig localConfig)
	{
		Log.i(LBSFunc.Log_Key, "updateConfig mcpttId = " + mcpttId + " localConfig = " + localConfig);
		this.isSerialReport = true;
		this.curMcpttId = mcpttId;

		int newLocationType = localConfig.getLocationType();
		boolean isSame = (newLocationType == curLocationType);
		Log.i(LBSFunc.Log_Key, "updateConfig isSame = " + isSame);

		setSerialReported(true,mcpttId);

		int timerInterval = localConfig.getTimeInterval();
		this.timer = new AlarmTimer(LBSService.this);
		timer.startRefreshTimer(timerInterval,mcpttId,this);

		if(!isSame)
		{
			stopLBS();
			startLBS(mcIdInfo,localConfig);
		}
		else
		{
			if(locServer != null)
			{
				locServer.updateConfig(ctx, localConfig);
			}
		}
	}*/

	public void stopLBS()
	{	
		Log.i(LBSFunc.Log_Key, "stopLBS ");
//		if(timer != null)
//		{
//			this.timer.stopRefreshTimer();
//			this.timer = null;
//		}
		//setSerialReported(false,null);
		if(locServer != null)
		{
			locServer.stopLBS();
			locServer = null;
		}
//		isSerialReport = false;

	}

	/*public void setSerialReported(boolean isSerialReported,String mcpttId) 
	{
		Log.i(LBSFunc.Log_Key, "setSerialReported isSerialReported = " + isSerialReported);
		if(operator != null
				&& locServer != null)
		{
			int result = CauseCont.OperateErr_Exception;

			result = operator.setSerialReported(isSerialReported,mcpttId);
			if(result != CauseCont.OperateCheck_Suc) 
			{
				String errStr = CauseCont.getOperateCheckErrStr(result,this);
				CommDlg.showToast(this,errStr);
				return;
			}
		}
	}*/
	
	
	public LocationData getCurLocation(){
		LocationData locData = LocationData.getDefaultLocationData();
		if(locServer != null){
			locData = locServer.getCurLocation();
		}
		
		return locData; 
	}
	
//	public VitalData getCurVitalData(){
//		VitalData vitalData = null;
//		if(locServer != null){
//			vitalData = locServer.getCurVitalData();
//			vitalData.setHeartRate(curHeartRate);
//		}
//		
//		return vitalData; 
//	}



//	public void setOperatoer(LBSOperator oper) {
//		this.operator = oper;
//
//	}
	
//	private void closeNotifyButton(){
//		int former = Settings.System.getInt(getContentResolver(), "main_screen_on_while_notify", 0);
//		if (former == 1) {// set turn screen on by code enable
//			Settings.System.putInt(getContentResolver(), "main_screen_on_while_notify", 0);
//		}
//	}
	
//	class NotifyObserver extends ContentObserver{
//
//		public NotifyObserver(Handler handler) {
//			super(handler);
//			// TODO Auto-generated constructor stub
//		}
//
//		@Override
//		public void onChange(boolean selfChange) {
//			// TODO Auto-generated method stub
//			super.onChange(selfChange);
//			Log.d("lq", "NotifyObserver onChange");
////			closeNotifyButton();
//		}
//
//	}

}
